package com.ruoyi.factorydata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.CacheSupportUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.MaterialCategory;
import com.ruoyi.factorydata.domain.vo.MaterialCategoryTreeVo;
import com.ruoyi.factorydata.mapper.MaterialCategoryMapper;
import com.ruoyi.factorydata.service.IMaterialCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 物料分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-07
 */
@Service
public class MaterialCategoryServiceImpl extends ServiceImpl<MaterialCategoryMapper, MaterialCategory> implements IMaterialCategoryService {
    @Autowired
    private MaterialCategoryMapper materialCategoryMapper;

    /**
     * 查询物料分类
     *
     * @param id 物料分类主键
     * @return 物料分类
     */
    @Override
    public MaterialCategory selectMaterialCategoryById(Long id) {
        return materialCategoryMapper.selectMaterialCategoryById(id);
    }

    /**
     * 查询物料分类列表
     *
     * @param materialCategory 物料分类
     * @return 物料分类
     */
    @Override
    public List<MaterialCategory> selectMaterialCategoryList(MaterialCategory materialCategory) {
        return materialCategoryMapper.selectMaterialCategoryList(materialCategory);
    }

    @Override
    public List<MaterialCategoryTreeVo> getMaterialCategoryTree(MaterialCategory materialCategory) {
        List<MaterialCategory> materialCategories = materialCategoryMapper.selectList(null);
        List<MaterialCategoryTreeVo> materialCategoryTreeVoList = new ArrayList<>();
        List<MaterialCategoryTreeVo> materialCategoryTreeVos = materialCategories.stream().map(item -> {
            return MaterialCategoryTreeVo.builder()
                    .id(item.getId())
                    .name(item.getMaterialCategoryName())
                    .parentId(0L)
                    .type("materialCategory")
                    .build();
        }).collect(Collectors.toList());
        MaterialCategoryTreeVo materialCategoryTreeVo = MaterialCategoryTreeVo.builder()
                .id(0L)
                .name("全部物料")
                .parentId(null)
                .type("root")
                .children(materialCategoryTreeVos)
                .build();
        materialCategoryTreeVoList.add(materialCategoryTreeVo);
        return materialCategoryTreeVoList;
    }

    /**
     * 新增物料分类
     *
     * @param materialCategory 物料分类
     * @return 结果
     */
    @Override
    public int insertMaterialCategory(MaterialCategory materialCategory) {
                materialCategory.setCreateTime(DateUtils.getNowDate());
            return materialCategoryMapper.insertMaterialCategory(materialCategory);
    }

    /**
     * 修改物料分类
     *
     * @param materialCategory 物料分类
     * @return 结果
     */
    @Override
    public int updateMaterialCategory(MaterialCategory materialCategory) {
                materialCategory.setUpdateTime(DateUtils.getNowDate());
        return materialCategoryMapper.updateMaterialCategory(materialCategory);
    }

    /**
     * 批量删除物料分类
     *
     * @param ids 需要删除的物料分类主键
     * @return 结果
     */
    @Override
    public int deleteMaterialCategoryByIds(Long[] ids) {
        int i = materialCategoryMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
        return i;
    }

    /**
     * 删除物料分类信息
     *
     * @param id 物料分类主键
     * @return 结果
     */
    @Override
    public int deleteMaterialCategoryById(Long id) {
        return materialCategoryMapper.deleteMaterialCategoryById(id);
    }

    @Override
    public String getNameByIdAndCache(Long materialCategory, HashMap<Long, String> categoryNameCache) {
        return CacheSupportUtils.getFromCacheOrDB(materialCategory, categoryNameCache,
                this::selectMaterialCategoryById,
                MaterialCategory::getMaterialCategoryName,
                true);
    }
}